package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UnionWarSessionReward;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionwar.UnionWarActivityModule;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_WAR_PREPARE_OVERVIEW,desc = "备战概览")
public class UnionWarPrepareOverviewCmd extends AbstractCommand {

    private int activityId;
    private int phase;
    private int beginSession;
    private int endSession;
    private UnionWarActivityServerProto.PrepareOverviewReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = UnionWarActivityServerProto.PrepareOverviewReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        if(reqMsg.hasBeginSession()) {
            beginSession = reqMsg.getBeginSession();
        }
        if(reqMsg.hasEndSession()) {
            endSession = reqMsg.getEndSession();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
        //if(StringUtils.isNullOrEmpty(unionUid)){
        //    unionUid = player.getUserInfo().getUnionUid();
        //}
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        CrossUnionWarProto.CrossPrepareOverviewReqMsg.Builder crossReq = CrossUnionWarProto.CrossPrepareOverviewReqMsg.newBuilder();
        crossReq.setActivityId(activityId);
        crossReq.setBeginSession(beginSession);
        crossReq.setEndSession(endSession);
        crossReq.setPhase(phase);
        if(phase == 2){
            UnionWar war = UnionPhaseWarMgr.getUnionWar(activityId);
            if(war != null){
                int all = war.getAllSession(2);
                for (int i = 1; i <= all; i++) {
                    UnionWarSessionReward reward = player.getModule(UnionWarActivityModule.class).getBattleReward(activityInfo.getActivityId(), 2, i);
                    if(reward != null && reward.isGetReward()){
                        CrossUnionWarProto.SessionGetRewardInfoMsg.Builder builder = CrossUnionWarProto.SessionGetRewardInfoMsg.newBuilder();
                        builder.setSession(i);
                        builder.setGetReward(true);
                        crossReq.addGetReward(builder);
                    }
                }
            }
        }
        player.sendUnionActivityGroupPacket(Protocol.C_UNION_WAR_PREPARE_OVERVIEW, crossReq, activityId);


        //ret = UnionPhaseWarMgr.prepareOverview(player, activityId, unionUid, phase, beginSession, endSession);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.PrepareOverviewRespMsg.Builder respMsg = UnionWarActivityServerProto.PrepareOverviewRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setPhase(phase);
            respMsg.setBeginSession(beginSession);
            respMsg.setEndSession(endSession);
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_WAR_PREPARE_OVERVIEW, respMsg);
        }
    }
}
